﻿using SqlSugar;
using SqlSugar.Extensions;
using System;

namespace ConsoleApp
{
    [SugarTable("oa_crm_phone_record")]
    public class PhoneRecordModel
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int id { get; set; }

        public string create_by { get; set; }
        public DateTime create_time { get; set; }
        public string modify_by { get; set; }
        public DateTime modify_time { get; set; }

        public string remark { get; set; }
        public int sort { get; set; }
        public int status { get; set; }

        public DateTime answer_date { get; set; }
        public string attribution { get; set; }
        public string call_duration { get; set; }
        public int call_type { get; set; }
        public string contact_name { get; set; }
        public string contact_phone { get; set; }
        public int department_id { get; set; }
        public int user_id { get; set; }
        public string user_name { get; set; }
        public string audio_path { get; set; }
        public DateTime cut_date { get; set; }
    }

    public class PhoneRecord
    {
        public bool Init()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=121.196.23.49;Database=crm_yddsj_net;Uid=root;Pwd=bca179e83f23af41;SslMode=none;AllowLoadLocalInfile=true;",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });

            try
            {
                var isSuccess = true;
                var list = db.Queryable<PhoneRecordModel>().ToList();
                foreach (var record in list)
                {
                    record.cut_date = record.answer_date.AddSeconds(record.call_duration.ObjToInt());

                    //var result = db.Updateable(record).UpdateColumns(x => new { x.cut_date }).ExecuteCommand();
                    //if (result <= 0)
                    //{
                    //    isSuccess = false;
                    //}
                }

                // 牛逼
                var successRow = db.Fastest<PhoneRecordModel>().BulkUpdate(list);
                Console.WriteLine(successRow);

                return isSuccess;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}